package com.fly.base.controller;

import com.fly.base.dao.LabelDao;
import com.fly.base.pojo.Label;
import com.fly.base.service.impl.LabelServiceImpl;
import entity.IdWorker;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/label")
public class LabelController {
    @Autowired
    LabelServiceImpl labelService;
    @Autowired
    private IdWorker idWorker;
    /**
     * 查询全部列表
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result<List> findAll() {
        return new Result(true, StatusCode.OK, "查询成功", labelService.findAll("allInformation"));
    }

    /**
     * 根据ID查询标签
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result<Label> findById(@PathVariable String id) {
        List list = new ArrayList();
        list.add(labelService.findById(id));
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 增加标签
     * 北京市昌平区建材城西路金燕龙办公楼一层 电话：400-618-9090
     * 5.2.3 功能测试
     * （1）测试查询全部数据
     * 使用浏览器测试GET方法 http://localhost:9001/label
     * （2）测试根据ID查询标签
     *
     * @param label
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Label label) {
        String key = idWorker.nextId() + "";
        labelService.add(label,key);
        return new Result(true, StatusCode.OK, "增加成功");
    }

    /**
     * 修改标签
     *
     * @param label
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@RequestBody Label label, @PathVariable String
            id) {
        labelService.update(label,id);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 删除标签
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable String id) {
        labelService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /*
    模糊查询
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result<Label> findSearch(@RequestBody Label label) {
        return new Result<Label>(true, StatusCode.OK, "查询成功", labelService.findSearch(label));
    }

    /**
     * 条件+分页查询
     * //     * @param searchMap
     *
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result<List> findSearch(@RequestBody Label label, @PathVariable int page, @PathVariable int size) {
        Page pageList = labelService.findSearch(label, page, size);
        List list = new ArrayList();
        list.add(new PageResult<Label>(pageList.getTotalElements(), pageList.getContent()));
        return new Result(true, StatusCode.OK, "查询成功", list);
    }
}